<%@ page contentType="text/html; charset=UTF-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="g" uri="http://geni-max.com/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<style>
.required {
	color: red;
}

label {
	padding: 5px;
}

#uploadDiv {
	background-color: #DEE0E0;
	border: 1px solid #9EAAB1;
}

#info {
	background-color: #FDFBDB;
	border: 1px dashed gray;
	width: 50%;
	margin: 5px;
}

.keyPoint {
	color: blue;
	font-style: italic;
}

.download {
	margin: 5px 5px 10px;
}

input {
	height: 23px;
}
</style>


<script type="text/javascript">
var nextIdx = 2;
function addMoreAttachment() {
  // var _clonedTr = $j("#attachment tr.upload:eq(0)").clone();
  // replaceTrValues(_clonedTr, nextIdx);

  $j('#attachment').append(createTr(nextIdx));
  nextIdx++;
  
  var size = $j('tr.upload').length;
  if (size >= 30) {
    $j("#addMoreBtn").attr("disabled", true);
    return;
  }
  
  $j("input[name='deleteBtn']").each(function(index) {
    $(this).enable();
  });
}

function createTr(rowNum) {
  var trStr = "<tr id='uploadTr" + rowNum + "' class='upload'>"
    + "<td nowrap='nowrap'><label>附件-" + rowNum + "</label><span class='required'>*</span></td>"
    + "<td><input type='file' id='file" + rowNum + "' name='upload' size='36' style='height:23px' onchange='selectFile(\"uploadTr" + rowNum + "\")'/></td>"
   
    + "<td><input type='button' value='删除' onclick='deleteTr(\"uploadTr" + rowNum + "\")'  class='button' name='deleteBtn' id='delete'/></td>"
    + "</tr>";
  return trStr;
}
function deleteTr(trId) {
  $j("#" + trId).remove();
  if ($j('tr.upload').length < 5) {
    $j("#addMoreBtn").removeAttr("disabled");
  } 
  if ($j('tr.upload').length == 1) {
    $j('tr.upload').find("#delete").attr("disabled", "true");
  }
}

function selectFile(trId) {
  var format = /(\.jpeg|\.jpg|\.gif|\.png|\.bmp|\.txt|\.html|\.htm|\.doc|\.xls|\.pdf|\.rar|\.zip)$/i;
  var fileName = $j("#" + trId).find("input[type='file']").val();
  if (fileName.lastIndexOf("\\") > 0) {
    fileName = fileName.substr(fileName.lastIndexOf("\\") + 1);
  }
  var fileLabel = $j("#" + trId).find("td:eq(0) label").html();
  if (fileName == "") {
    alert("[" + fileLabel + "]文件不能为空，请选择要上传的文件");
    return false;
  } else if (!format.test(fileName)) {
    alert("[" + fileLabel + "]文件类型不符，请重新选择符合规定类型的文件");
    return false;
  } else {
    var idx = fileName.indexOf("_");
    if (idx > 0) {
      $j("#" + trId).find("select").attr("value", fileName.substr(0, 2));
      $j("#" + trId).find("input[name='orderNum']").attr("value", fileName.substring(0, idx));
    } else if (fileName.indexOf("SO") == 0 || fileName.indexOf("TO") == 0) {
      $j("#" + trId).find("select").attr("value", fileName.substr(0, 2));
      $j("#" + trId).find("input[name='orderNum']").attr("value", fileName.substring(0, fileName.indexOf(".")));      
    }
    return true;
  }
}

function checkFile(trId) {
  var format = /(\.jpeg|\.jpg|\.gif|\.png|\.bmp|\.txt|\.html|\.htm|\.doc|\.xls|\.pdf|\.rar|\.zip)$/i;
  var fileName = $j("#" + trId).find("input[type='file']").val();
  var fileLabel = $j("#" + trId).find("td:eq(0) label").html();
  if (fileName == "") {
    alert("[" + fileLabel + "]文件不能为空，请选择要上传的文件");
    return false;
  } else if (!format.test(fileName)) {
    alert("[" + fileLabel + "]文件类型不符，请重新选择符合规定类型的文件");
    return false;
  } else {
    return true;
  }
}

function checkAttachmentTr(trId) {
  if (!checkFile(trId)) {
    return false;
  }
 
  return true;
}
function ajaxValidatOrder() {
  $j.post('ValidateOrders.bl', $j("#attachForm"),
    function(data) {
      alert(data);
    }
  );
}

function checkBeforeSubmit() {
  var valid = true;
  $j("tr.upload").each(function(index) {
    if (!valid) {
      return;
    }
    if (!checkAttachmentTr($(this).id)) {
      valid = false;
      return;
    }
  });
  
  return valid; 
  
}



function closeInfoDialog() {
  $j("#info").remove();
}


$j(document).ready(function() {
  var isSuccess = <s:property value="success"/>;
  if (isSuccess) {
    if(confirm("附件上传成功,是否继续为其它订单做电子回单?")) {
       window.location = "ListSOPOD.bl";
    } else{
       window.location = "${pageContext.request.contextPath}/module/upload/MaintainFile.bl";
    }
  }
});

</script>

<div id="statusbar"><span class="spanfont"> <g:text
	name="%{getText('current.position')}" /><g:text
	name="%{getText('so.title.single.auto.pod')}" /> </span></div>


<s:form id="attachForm" namespace="/module/order" action="UploadPODFile"
	method="post" enctype="multipart/form-data">
	<g:token name="salesOrderAutoPOD" />
	<g:hidden name="salesOrderId" value="%{salesOrderId}"></g:hidden>
	<g:panel>
		<g:grid cols="4" border="0"
			colCssClassList="{'col1', 'col2', 'col3', 'col4'}">
			<g:fieldgroup key="so.salesOrderNumber">
				<g:label name="salesOrderVO.salesOrderNumber" />
			</g:fieldgroup>
			<g:label key="so.custmer.code" name="salesOrderVO.customer.code" />
			<g:newline />

		</g:grid>

		<table id="attachment" class="wwFormTable">
			<tr>

				<td colspan="3"><input type="button" id="addMoreBtn"
					onclick="addMoreAttachment()" value="添加更多附件" class="button" /></td>
			</tr>
			<tr id="uploadTr1" class="upload">
				<td nowrap="nowrap"><s:label value="附件-1" theme="simple" /><span
					class="required">*</span></td>
				<td><s:file id="file1" name="upload" size="36"
					onchange="selectFile('uploadTr1')" cssStyle="height: 23px"
					theme="simple" /></td>
				<td><input id="delete" name="deleteBtn" type="button"
					onclick="deleteTr('uploadTr1')" value="删除" disabled="disabled"
					class="button" /></td>
			</tr>
		</table>
		
		<br>
	</g:panel>
	<g:panel>
		<g:toolbar>
			<g:toolbutton type="submit" action="ListSOPOD"
				value="%{getText('so.back')}" />
			<g:toolbutton type="submit" onclick="return checkBeforeSubmit()"
				value="%{getText('so.submit')}"  />
		</g:toolbar>
	</g:panel>
</s:form>

